/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fallen.deck.front_end;

import fallen.deck.front_end.jCustomTabbed.DeckMetadata;
import java.awt.BasicStroke;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.Rotation;

/**
 *
 * @author Santiago Moreno
 */
public class StadisticsWindow extends javax.swing.JDialog {

    private XYSeries energySeries, energySeriesAcum;
    final DefaultPieDataset typesPieData, allyPieData, weaponsPieData, ritualPieData;

    /**
     * Creates new form StadisticsWindow
     */
    public StadisticsWindow(DeckMetadata metadata) {
        this.setIconImage(new ImageIcon(getClass().getResource("/icons/16/chart_bar.png")).getImage());
        initComponents();
        energySeries = new XYSeries("Cartas Habilitadas por Turnos");
        energySeriesAcum = new XYSeries("Curva de Energía");
        int acumCost = 0;
        for (int i = 0; i < metadata.getCost().length; i++) {
            acumCost += metadata.getCost()[i];
            energySeries.add(i, metadata.getCost()[i]);
            energySeriesAcum.add(i, acumCost);
        }
        // create Types PieChart a dataset...
        typesPieData = new DefaultPieDataset();
        allyPieData = new DefaultPieDataset();
        weaponsPieData = new DefaultPieDataset();
        ritualPieData = new DefaultPieDataset();
        //Loading the data
        fillPieData(metadata.getTypes(), typesPieData);
        fillPieData(metadata.getSupertypes().get("Aliado"), allyPieData);
        fillPieData(metadata.getSupertypes().get("Arma"), weaponsPieData);
        fillPieData(metadata.getSupertypes().get("Ritual"), ritualPieData);
        //Updating the Charts Label
        updatePieChart3D("Tipos", typesPieData, lblTypesChart);
        updatePieChart3D("Aliados", allyPieData, lblAllyChart);
        updatePieChart3D("Armas", weaponsPieData, lblWeaponsChart);
        updatePieChart3D("Rituales", ritualPieData, lblRitualChart);
        updateManaCurve();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane = new javax.swing.JTabbedPane();
        pnlMana = new javax.swing.JPanel();
        lblManaCurve = new javax.swing.JLabel();
        pnlDistribution = new javax.swing.JPanel();
        lblTypesChart = new javax.swing.JLabel();
        lblAllyChart = new javax.swing.JLabel();
        lblWeaponsChart = new javax.swing.JLabel();
        lblRitualChart = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Estadísticas");
        setIconImage(null);
        setModal(true);
        setResizable(false);

        jTabbedPane.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        lblManaCurve.setText("[chart]");

        javax.swing.GroupLayout pnlManaLayout = new javax.swing.GroupLayout(pnlMana);
        pnlMana.setLayout(pnlManaLayout);
        pnlManaLayout.setHorizontalGroup(
            pnlManaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblManaCurve, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
        );
        pnlManaLayout.setVerticalGroup(
            pnlManaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblManaCurve, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
        );

        jTabbedPane.addTab("Curva de Energía", pnlMana);

        lblTypesChart.setText("Types Chart");

        lblAllyChart.setText("AllyChart");

        lblWeaponsChart.setText("WeaponChart");

        lblRitualChart.setText("Ritual Chart");

        javax.swing.GroupLayout pnlDistributionLayout = new javax.swing.GroupLayout(pnlDistribution);
        pnlDistribution.setLayout(pnlDistributionLayout);
        pnlDistributionLayout.setHorizontalGroup(
            pnlDistributionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDistributionLayout.createSequentialGroup()
                .addGroup(pnlDistributionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTypesChart, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                    .addComponent(lblRitualChart, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDistributionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblWeaponsChart, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                    .addComponent(lblAllyChart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        pnlDistributionLayout.setVerticalGroup(
            pnlDistributionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDistributionLayout.createSequentialGroup()
                .addGroup(pnlDistributionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lblTypesChart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                    .addComponent(lblAllyChart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDistributionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblWeaponsChart, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                    .addComponent(lblRitualChart, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)))
        );

        jTabbedPane.addTab("Distribución de Cartas", pnlDistribution);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 837, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-839)/2, (screenSize.height-375)/2, 839, 375);
    }// </editor-fold>//GEN-END:initComponents

    private void updateManaCurve() {

        XYSeriesCollection xyData = new XYSeriesCollection();
        xyData.addSeries(energySeries);
        xyData.addSeries(energySeriesAcum);

        JFreeChart chart = ChartFactory.createXYLineChart(
                "",
                "Turno",
                "Cant. Cartas",
                xyData,
                PlotOrientation.VERTICAL,
                true,
                false,
                false);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.getRenderer().setStroke(new BasicStroke(2));
        BufferedImage image = chart.createBufferedImage(650, 320);

        lblManaCurve.setIcon(new ImageIcon(image));
        lblManaCurve.setText("");
    }
    
    private void updatePieChart3D(String title, PieDataset data, JLabel label )
    {
        JFreeChart chart = ChartFactory.createPieChart3D(title,
                data,
                false,
                true,
                false);
        PiePlot3D plot = (PiePlot3D)chart.getPlot();
        plot.setStartAngle(270);
        plot.setDirection(Rotation.ANTICLOCKWISE);
        plot.setForegroundAlpha(0.60f);
        plot.setInteriorGap(0.33);
        BufferedImage image = chart.createBufferedImage(325, 160);

        label.setIcon(new ImageIcon(image));
        label.setText("");
    }
    
    private void fillPieData(HashMap source, DefaultPieDataset dataset)
    {
        System.out.println(source.toString());
        for (Iterator it = source.entrySet().iterator(); it.hasNext();) {
            Map.Entry e = (Map.Entry) it.next();
            if ((Integer)e.getValue() > 0)
                dataset.setValue((String)e.getKey(), (Integer)e.getValue());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JLabel lblAllyChart;
    private javax.swing.JLabel lblManaCurve;
    private javax.swing.JLabel lblRitualChart;
    private javax.swing.JLabel lblTypesChart;
    private javax.swing.JLabel lblWeaponsChart;
    private javax.swing.JPanel pnlDistribution;
    private javax.swing.JPanel pnlMana;
    // End of variables declaration//GEN-END:variables
}
